package com.sangeng.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.sangeng.constant.enums.AppHttpCodeEnum;
import com.sangeng.domain.core.ResponseResult;
import com.sangeng.domain.dto.CategoryPageDto;
import com.sangeng.domain.dto.CategoryAddDto;
import com.sangeng.domain.entity.Category;
import com.sangeng.domain.vo.CategoryUpdateVo;
import com.sangeng.domain.vo.ExcelCategoryVo;
import com.sangeng.service.CategoryService;
import com.sangeng.utils.BeanUtil;
import com.sangeng.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory() {
        return categoryService.listAllCategory();
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        // 设置响应头
        try {
            // 设置下载文件的相应头
            WebUtils.setDownLoadHeader("分类.xlsx", response);
            // 获取所有分类
            List<ExcelCategoryVo> exportList = BeanUtil.copyBeanList(categoryService.list(), ExcelCategoryVo.class);

            // 把数据写道excel中
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(exportList);

        }
        catch (Exception  e){
            // 出现异常相应json
            e.printStackTrace();
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    @PreAuthorize("@ps.hasPermission('content/category/index')")
    @GetMapping("/list")
    public ResponseResult list(CategoryPageDto categoryPageDto) {
        return categoryService.listAllCategoryPage(categoryPageDto);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryAddDto categoryAddDto) {
        return categoryService.addCategory(categoryAddDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable Long id) {
        return ResponseResult.okResult(BeanUtil.copyBean(categoryService.getById(id), CategoryUpdateVo.class));
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryUpdateVo categoryUpdateVo) {
        return categoryService.updateById(BeanUtil.copyBean(categoryUpdateVo, Category.class)) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable Long id) {
        return categoryService.removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody CategoryUpdateVo categoryUpdateVo) {
        return categoryService.updateById(BeanUtil.copyBean(categoryUpdateVo, Category.class)) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }
}
